﻿import { Component, Injector, ViewChild ,Input} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { RequirementPackageDto } from '@shared/service-proxies/service-proxies';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { Tab } from './tab.model';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementItemEditComponent } from './requirement-item_edit.component'
import { RequirementPackageServiceProxy, RequirementItemDto,StandardDetailType } from '@shared/service-proxies/service-proxies';

@Component({
    selector: 'requirementTabComponent',
    templateUrl: './requirement_tab.component.html',
    styleUrls: ['./requirement_tab.component.less'],
    animations: [appModuleAnimation()]
})
export class RequirementTabComponent extends AppComponentBase{
    @ViewChild('requirementItemEditComponent') requirementItemEditComponent: RequirementItemEditComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    tabs: Array<Tab>;
    tabObject:{
        filterText:""
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';

    active =false;
    hidden=true;
    title:"hello";
    @Input() requirementPackage: RequirementPackageDto;

    constructor(
        injector: Injector,
        private requirementPackageServiceProxy: RequirementPackageServiceProxy,
        ) {
        super(injector);
        this.active = false;
    }
    addTab(requirementItemDto: RequirementItemDto): void {
        this.active = true;
    }
   /**
    * 弹出添加需求详细页面
    * 弹出编辑需求详细页面
    * @param requirementItemDto 
    */
    createOrEditRequirementItem(requirementItemDto : RequirementItemDto):void{
        this.requirementItemEditComponent.show(this.requirementPackage, requirementItemDto);
    }
    deleteRequirementPackage(requirementItemDto :RequirementItemDto): void {
        this.message.confirm(
            '需求包将被删除',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this.requirementPackageServiceProxy.deleteItemsOfPackage(requirementItemDto.id)
                    .pipe(finalize(() => { this.getRequirementItems();}))
                    .subscribe(() => {
                        this.notify.info(this.l('DeletedSuccessfully'));
                    });
                }
            }
        );
    }
    setRequirementPackage(_pRequirementPackage: RequirementPackageDto): void {
        this.requirementPackage = _pRequirementPackage;
    }
    getStandarType(standardType:string):string{
        return StandardDetailType[standardType];
    } 
    /**
    * 检索需求详细
    * @param requirementItemDto 
    */
    getRequirementItems(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this.requirementPackageServiceProxy.getItemsOfPackage(this.requirementPackage.id)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                if(!result){
                    result = [];
                }
                this.primengTableHelper.defaultRecordsCountPerPage = result.length;
                this.primengTableHelper.totalRecordsCount = result.length;
                this.primengTableHelper.records = result;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    /**
     * 删除需求详细
     * @param requirementItemDto 
     */
    deleteRequirementItems(requirementItemDto:RequirementItemDto){
        this.message.confirm(
            '删除该需求？',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this.requirementPackageServiceProxy.deleteItemsOfPackage(requirementItemDto.id)
                    .pipe(finalize(() => { this.getRequirementItems();}))
                    .subscribe(() => {
                        this.notify.info(this.l('DeletedSuccessfully'));
                    });
                }
            }
        );
    }
    reloadPage():void {
        var ddd ='';
    }

    downloadWebLogs = function () {
        const self = this;
        self._webLogService.downloadWebLogs().subscribe((result) => {
            self._fileDownloadService.downloadTempFile(result);
        });
    };

    onResize(event): void {
        
    }

    ngOnInit(): void {
        const self = this;
    }
}
